//
//  HomeRootTopHeaderView.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2022/8/29.
//

#import "HomeRootTopHeaderView.h"

@interface HomeRootTopHeaderView ()

@property (weak, nonatomic) IBOutlet YYAnimatedImageView *bigImage;
@property (weak, nonatomic) IBOutlet UILabel *gameNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *gameSubNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *gameTypeLabel1;
@property (weak, nonatomic) IBOutlet UILabel *gameTypeLabel2;
@property (weak, nonatomic) IBOutlet UIView *gameSignView;
@property (weak, nonatomic) IBOutlet UIView *gameInfoView;
@property (weak, nonatomic) IBOutlet UIView *functionView;

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation HomeRootTopHeaderView

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self.functionView removeAllSubviews];
    NSArray *functions = [LocalData getMenudicWithKey:@"home_function"];
    UIView *temsignOneView = nil;
    for (NSInteger i = 0; i < functions.count; i++) {
        NSDictionary *dict = functions[i];
        UIView *signOneView = [[UIView alloc] init];
        [self.functionView addSubview:signOneView];
        
        UIImageView *rightImage = [[UIImageView alloc] init];
        [rightImage yy_setImageWithURL:[NSURL URLWithString:dict[@"img"]] placeholder:placeholder_image];
        [signOneView addSubview:rightImage];
        
        UILabel *signLabel = [[UILabel alloc] init];
        signLabel.text = dict[@"title"];
        signLabel.font = [UIFont systemFontOfSize:12];
        signLabel.textColor = kColor333333;
        signLabel.textAlignment = NSTextAlignmentCenter;
        [signOneView addSubview:signLabel];
        
        [signOneView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.top.equalTo(self.functionView);
            if (temsignOneView) {
                make.left.equalTo(temsignOneView.mas_right);
            } else {
                make.left.equalTo(self.functionView).offset(5);
            }
            make.width.mas_equalTo((SL_WINDOW_WIDTH-10)/functions.count);
        }];
        [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.top.equalTo(signOneView);
            make.left.equalTo(signOneView).offset(10);
            make.width.equalTo(rightImage.mas_height);
        }];
        [signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.left.equalTo(signOneView);
            make.top.equalTo(rightImage.mas_bottom).offset(10);
        }];
        temsignOneView = signOneView;
    }
    
    CALayer *bottom_layer = [self.bottomView.layer.sublayers objectAtIndex:0];
    if ([bottom_layer isMemberOfClass:[CAGradientLayer class]]) {
        [bottom_layer removeFromSuperlayer];
    }
    CAGradientLayer *bottom_grad = [CAGradientLayer layer];
    bottom_grad.frame = self.bottomView.bounds;
    bottom_grad.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
    [self.bottomView.layer insertSublayer:bottom_grad atIndex:0];
    
    self.gameInfoView.layer.shadowOffset = CGSizeMake(0,0);
    self.gameInfoView.layer.masksToBounds = NO;
    self.gameInfoView.layer.shadowOpacity = 1;
    self.gameInfoView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.10].CGColor;
    self.gameInfoView.layer.shadowRadius = 8;
}


- (void)initCellData:(GameDetailNewModel *)detailModel
{
    [self.bigImage yy_setImageWithURL:[NSURL URLWithString:detailModel.gameMainIcon] placeholder:placeholder_image];
    self.gameNameLabel.text = detailModel.gameName;
    self.gameSubNameLabel.text = [NSString stringWithFormat:@" %@  ", detailModel.gameSubName];
    
    [self.gameSignView removeAllSubviews];
    UIView *temsignOneView = nil;
    NSInteger count = detailModel.gameSignArr.count;
    if (count > 3) count = 3;
    for (NSInteger i = 0; i < count; i++) {
        NSString *signStr = detailModel.gameSignArr[i];
        UIView *signOneView = [[UIView alloc] init];
        signOneView.backgroundColor = UIColorHex(0xEBEBEB);
        signOneView.layer.cornerRadius = 4;
        signOneView.layer.masksToBounds = YES;
        [self.gameSignView addSubview:signOneView];
        UILabel *signLabel = [[UILabel alloc] init];
        signLabel.text = signStr;
        signLabel.font = [UIFont systemFontOfSize:12];
        signLabel.textColor = kColor888888;
        [signOneView addSubview:signLabel];
        [signOneView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.gameSignView).offset(5);
            if (temsignOneView) {
                make.left.equalTo(temsignOneView.mas_right).offset(5);
            } else {
                make.left.equalTo(self.gameSignView);
            }
        }];
        [signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(signOneView);
            make.top.equalTo(signOneView).offset(2);
            make.left.equalTo(signOneView).offset(3);
        }];
        temsignOneView = signOneView;
    }
    
    self.gameTypeLabel1.text = [NSString stringWithFormat:@"%@分", detailModel.gameScore];
    
    self.gameTypeLabel2.text = [detailModel.gameTypes componentsJoinedByString:@"|"];
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    self.height = CGRectGetMaxY(self.functionView.frame);
    
}

- (void)showshadow:(UIColor *)color
{
    CALayer *top_layer = [self.topView.layer.sublayers objectAtIndex:0];
    if ([top_layer isMemberOfClass:[CAGradientLayer class]]) {
        [top_layer removeFromSuperlayer];
    }
    CAGradientLayer *top_grad = [CAGradientLayer layer];
    top_grad.frame = self.topView.bounds;
    top_grad.colors = [NSArray arrayWithObjects:(id)[color CGColor], (id)[[UIColor clearColor] CGColor], nil];
    [self.topView.layer insertSublayer:top_grad atIndex:0];
    
    
}




@end
